<?php

namespace Richang;

use core\controller\baseController;
use common\Factory\Factory;
use common\Table\InterfacePvTable;

class RichangController extends baseController {
    
    public function index() {
        $this->view();
    }
    
    public function interfacePv() {
        $redis = Factory::getServer('Redis');
        if($redis) {
            $date = date('Ymd', strtotime('-1 day'));
            $items = ['ad_act_pv', 'ad_click_pv', 'ad_query_pv'];
            $count = 0;
            foreach($items as $item) {
                $result = $redis->hGet($item, $date);
                if($result !== false) {
                    $count += $result;
                    $table = new InterfacePvTable();
                    $table->insert(['interface_name' => $item, 'date' => $date, 'count' => $result]);
                    $redis->hDel($item, $date);
                } 
            }
            $table->insert(['interface_name' => 'count_pv', 'date' => $date, 'count' => $count]);
        }
    }
    
    public function interfacePvQuery() {
        $dateLength = array();
        $table = new InterfacePvTable();
        $startDate = date('Ymd', strtotime('-1 week'));
        $endDate = date('Ymd', strtotime('-1 day'));
        $result = $table->getDb()->selectAll('select interface_name, count from interface_pv where interface_name = "count_pv" and date >= '. $startDate .' and date <= ' . $endDate);
        $maxResult = $table->getDb()->selectOne('select max(count) as count from interface_pv where interface_name = "count_pv"');

        for($i = 1; $i <= 7; $i++) {
            $dateLength[] = date('Ymd', strtotime("-$i day"));
        }
        asort($dateLength);
        
        $this->view(['dataLength' => $dateLength, 'result' => $result, 'max' => $maxResult['count']]);
    }
    
    public function redisInfo() {
        $redis = Factory::getServer('Redis');
        $info = $redis->info();

        $this->view(['idfaCount' => $redis->ssize('idfaList'), 'max' => $info['used_memory_human'], 'maxPeak' => $info['used_memory_peak_human']]);
    }
}